package com.wwy.arithmetic.newsuanfa;

import java.util.*;

/**
 * @author 王伟羽
 * @program: arithmetictest
 * @description: 无重复的最长字符串
 * @create: 2025/03/31
 */

public class wuchongfuzifuchuan {

    public int lengthOfLongestSubstring(String s) {
        char[] arr = s.toCharArray();
        int max = 0;
        int left = 0;
        int length = 0;
        for (int right = 0; right < arr.length; right++) {
            for (int k = left; k < right; k++) {
                if (arr[right] == arr[k]) {
                    left = k + 1;
                    length = right - left;
                    break;
                }
            }
            length++;
            max = Math.max(max, length);
        }
        return max;
    }

    public int baolipojie(String s) {
        if (s == null || s == "") {
            return 0;
        }
        char[] chars = s.toCharArray();
        Map<Character, Integer> map = new HashMap<>();
        Set<Character> set = new HashSet<>();
        Deque<Character> deque = new LinkedList<>();
        int max = 0;
        int a = 0;
        for (int i = 0; i < chars.length; i++) {
            if (set.add(chars[i])) {
                map.put(chars[i], i);
                deque.push(chars[i]);
                a++;
            } else {
                max = Math.max(a, max);
                while (!deque.isEmpty()) {
                    Character pop = deque.pollLast();
                    set.remove(pop);
                    if (pop == chars[i]) {
                        a = i - map.get(chars[i]);
                        deque.push(chars[i]);
                        set.add(chars[i]);
                        map.put(chars[i], i);
                        break;
                    }
                }
            }
        }
        return Math.max(a, max);
    }

}
